using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using nmspLIBRO;
using nmspLIBROENINVENTARIO;
using nmspUSUARIO;

public partial class loggedin_Agregar : System.Web.UI.Page
{
    LibroEnInventario lib = new LibroEnInventario();
    protected void Page_Load(object sender, EventArgs e)
    {
        string userName = User.Identity.Name;
        //SqlDataSource1.SelectCommand =  "SELECT [Titulo], [Autor], [id_Editorial], [ImagenLibro], [Disponible] FROM [tblLibros] INNER JOIN  aspnet_Users ON tblLibros.UserId = aspnet_Users.UserId WHERE (aspnet_Users.UserName = '" + userName + "')";
        string sql = "SELECT tblLibros.id, tblLibros.Titulo, tblLibros.Autor, tblEditoriales.Editorial, tblLibros.ImagenLibro, tblLibros.Disponible ";
        sql += "FROM   tblLibros INNER JOIN ";
        sql += "tblEditoriales ON tblLibros.id_Editorial = tblEditoriales.id INNER JOIN ";
        sql += "aspnet_Users ON tblLibros.UserId = aspnet_Users.UserId ";
        sql += "WHERE  (aspnet_Users.UserName = '" + userName + "') ORDER BY tblLibros.Fe_Carga DESC";
        SqlDataSource1.SelectCommand = sql;

        string js = "if ((event.which && event.which == 13) || "
            + "(event.keyCode && event.keyCode == 13)) "
            + "{return false;} "
            + "else return true;";
        GridView1.Attributes.Add("onkeydown", js);


    }

    protected void btnAgregar_Click(object sender, EventArgs e)
    {

        //No debe tener caracteres invalidos para el sql
        if ( !(this.txtTitulo.Text.Contains("'")) &&
            !(this.txtUrlFoto.Text.Contains("'")) &&
            !(this.txtAutor.Text.Contains("'")) &&
            !(this.txtEditorial.Text.Contains("'")) )
        {
            LibroEnInventario libInventario = new LibroEnInventario(this.txtTitulo.Text, this.txtUrlFoto.Text, this.txtAutor.Text, this.txtEditorial.Text);
            Usuario usr = new Usuario(User.Identity.Name);
            libInventario.grabarNuevoLibroEnInventario(usr);
        
            Response.Redirect("Agregar.aspx");
        }

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }


    /////
    protected string SetImage(object input)
    {
        string returnUrl;

        string imageUrl = input.ToString();
        string noImageUrl = "~/images/imagen-no-disponible.jpg";
        if (!String.IsNullOrEmpty(input.ToString()))
        {
            returnUrl = imageUrl;
        }
        else
        {
            returnUrl = noImageUrl;
        }
        return returnUrl;
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Response.Redirect("Agregar.aspx");
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
            Label lblId = (Label)GridView1.Rows[e.RowIndex].FindControl("lblIdForDeletion");
            this.lib.EliminarLibroEnInventario(Int32.Parse(lblId.Text));
            Response.Redirect("Agregar.aspx");
       
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
       
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox vtxtTitulo = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTitulo");
        TextBox vtxtAutor = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAutor");
        TextBox vtxtEditorial = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditorial");
        TextBox vtxtUrlFoto = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtUrlFoto");
        DropDownList vcmbDisponible = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("cmbDisponible");
        Label lblId = (Label)GridView1.Rows[e.RowIndex].FindControl("lblId");


        //No debe tener caracteres invalidos para el sql
        if (!(vtxtTitulo.Text.Contains("'")) &&
        !(vtxtUrlFoto.Text.Contains("'")) &&
        !(vtxtAutor.Text.Contains("'")) &&
        !(vtxtEditorial.Text.Contains("'")))
        {
            lib.modificarLibroEnInventario(Int32.Parse(lblId.Text), vtxtTitulo.Text, vtxtUrlFoto.Text, vtxtAutor.Text, vtxtEditorial.Text, vcmbDisponible.SelectedValue);

            GridView1.EditIndex = -1;

            Response.Redirect("Agregar.aspx");
        }
    }
    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}
